package com.zqh.myfmmall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.dao.ShoppingCartDao;
import com.zqh.myfmmall.entity.Product;
import com.zqh.myfmmall.entity.ProductSku;
import com.zqh.myfmmall.entity.ShoppingCart;
import com.zqh.myfmmall.service.ProductImgService;
import com.zqh.myfmmall.service.ProductService;
import com.zqh.myfmmall.service.ProductSkuService;
import com.zqh.myfmmall.service.ShoppingCartService;
import com.zqh.myfmmall.vo.ShoppingCartVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车 (ShoppingCart)表服务实现类
 *
 * @author KONO 曾淇杭哒！
 * @since 2022-01-20 20:53:00
 */
@Service("shoppingCartService")
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {
    @Resource
    private ProductSkuService productSkuService;
    @Resource
    private ProductService productService;
    @Resource
    private ProductImgService productImgService;

    @Override
    public List<ShoppingCartVO> getVosByUserId(String userId) {
        ArrayList<ShoppingCartVO> shoppingCartVOS = new ArrayList<>();
        List<ShoppingCart> shoppingCarts = query().eq("user_id", userId).list();
        for (ShoppingCart shoppingCart : shoppingCarts) {
            Product product = productService.getById(shoppingCart.getProductId());
            ProductSku sku = productSkuService.getById(shoppingCart.getSkuId());
            String mainImg = productImgService.getMainImgByProductId(shoppingCart.getProductId());
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO(shoppingCart, sku, product, mainImg);
            shoppingCartVOS.add(shoppingCartVO);
        }
        return shoppingCartVOS;
    }

    @Override
    public List<ShoppingCartVO> getVosByCartIds(List<String> cartIds) {
        ArrayList<ShoppingCartVO> shoppingCartVOS = new ArrayList<>();
        List<ShoppingCart> shoppingCarts = listByIds(cartIds);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            Product product = productService.getById(shoppingCart.getProductId());
            ProductSku sku = productSkuService.getById(shoppingCart.getSkuId());
            String mainImg = productImgService.getMainImgByProductId(shoppingCart.getProductId());
            ShoppingCartVO shoppingCartVO = new ShoppingCartVO(shoppingCart, sku, product, mainImg);
            shoppingCartVOS.add(shoppingCartVO);
        }
        return shoppingCartVOS;
    }


}

